package com.swpu.service_remind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import com.swpu.service_remind.entity.DrinkRemind;
import com.swpu.service_remind.mapper.DrinkRemindMapper;
import com.swpu.service_remind.service.DrinkRemindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-10
 */
@Service
public class DrinkRemindServiceImpl extends ServiceImpl<DrinkRemindMapper, DrinkRemind> implements DrinkRemindService {

    @Override
    public void saveUserDrinkRemindSetting(String openid, DrinkRemind drinkRemind) {
        LambdaQueryWrapper<DrinkRemind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrinkRemind::getUserId,openid);
        DrinkRemind remind = this.getOne(queryWrapper);
        if(remind==null){
            drinkRemind.setUserId(openid);
            drinkRemind.setEnableWx(1);
            drinkRemind.setEnableSms(0);
            drinkRemind.setStatus(1);
            boolean isSave = save(drinkRemind);
            if(!isSave){
                throw new RuntimeException("用户喝水提醒保存失败");
            }
        }else {
            remind.setDuration(drinkRemind.getDuration());
            remind.setStartHour(drinkRemind.getStartHour());
            remind.setEndHour(drinkRemind.getEndHour());
            remind.setEnableWx(drinkRemind.getEnableWx());
            remind.setEnableSms(drinkRemind.getEnableSms());
            remind.setStatus(drinkRemind.getStatus());
            this.update(remind,null);
        }

    }
    @Override
    public DrinkRemind getDrinkRemindSetting(String openid) {
        LambdaQueryWrapper<DrinkRemind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrinkRemind::getUserId,openid);
        DrinkRemind drinkRemind = this.getOne(queryWrapper);
        return drinkRemind;
    }
}
